# Replication package for 
# "International Third Parties and the Implementation of 
# Comprehensive Peace Agreements After Civil War"
# Johannes Karreth, Jason Quinn, Madhav Joshi, and Jaroslav Tir
# Forthcoming in the Journal of Conflict Resolution
# jkarreth@ursinus.edu

# Please read "0_readme.html" before using this code.

# This file is `2_analyze_implementation.R`
# It generates the main results in the article and in the supporting information.

# Unit of obs.: one post-CPA year, up to 10 years total per CPA
#               starting in 1989, since PAM data start then

####################################
### Set up data and packages
####################################

# Set your working directory to the replication package

# setwd("...")
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

# Load R packages

library("lme4")
library("tidyverse")
library("texreg")
library("estimatr")
library("ggridges")

# Install custom functions (from my Github, or from the local replication package)

devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/extract.panelAR.R")
# source(file = "Software/extract.panelAR.R")
devtools::source_url("https://raw.githubusercontent.com/jkarreth/JKmisc/master/theme_jk.R")
# source(file = "Software/theme_jk.R")

# Install the panelAR package (from the author's Github, or from the local replication package)

# remotes::install_github(repo = "kkashin/panelAR")
# install.packages("Software/panelAR_0.1.tar.gz", type= "source", repos= NULL)
library("panelAR")

meanstd <- function(x){
  (x - mean(x, na.rm = TRUE)) / (2*sd(x, na.rm = TRUE))
}

# Load data (see readme for sources)

d <- rio::import("Data/Work/implementation_allyears.csv")

# Fix typos in PAM and adjust date formatting by hand

d[d$pam_caseid == 1, ]$pam_country_accorddate <- "Guatemala (1996-12-29)"
d[d$pam_caseid == 2, ]$pam_country_accorddate <- "El Salvador (1991-12-31)"
d[d$pam_caseid == 3, ]$pam_country_accorddate <- "Northern Ireland (1998-04-10)"
d[d$pam_caseid == 4, ]$pam_country_accorddate <- "Macedonia (2001-08-13)"
d[d$pam_caseid == 5, ]$pam_country_accorddate <- "Croatia (1995-11-12)"
d[d$pam_caseid == 6, ]$pam_country_accorddate <- "Bosnia (1995-11-21)"
d[d$pam_caseid == 7, ]$pam_country_accorddate <- "Guinea-Bissau (1998-01-11)"
d[d$pam_caseid == 8, ]$pam_country_accorddate <- "Mali (1991-01-06)"
d[d$pam_caseid == 9, ]$pam_country_accorddate <- "Senegal (2004-12-30)"
d[d$pam_caseid == 10, ]$pam_country_accorddate <- "Niger (1995-04-15)"
d[d$pam_caseid == 11, ]$pam_country_accorddate <- "Ivory Coast (2007-03-04)"
d[d$pam_caseid == 12, ]$pam_country_accorddate <- "Liberia (2003-08-18)"
d[d$pam_caseid == 13, ]$pam_country_accorddate <- "Sierra Leone (1996-11-30)"
d[d$pam_caseid == 14, ]$pam_country_accorddate <- "Sierra Leone (1999-07-07)"
d[d$pam_caseid == 15, ]$pam_country_accorddate <- "Congo-Brazzaville (1999-12-29)"
d[d$pam_caseid == 16, ]$pam_country_accorddate <- "Burundi (2000-08-28)"
d[d$pam_caseid == 17, ]$pam_country_accorddate <- "Rwanda (1993-08-04)"
d[d$pam_caseid == 18, ]$pam_country_accorddate <- "Djibouti (1994-12-26)"
d[d$pam_caseid == 19, ]$pam_country_accorddate <- "Djibouti (2001-05-12)"
d[d$pam_caseid == 20, ]$pam_country_accorddate <- "Angola (1994-11-15)"
d[d$pam_caseid == 21, ]$pam_country_accorddate <- "Angola (2002-04-04)"
d[d$pam_caseid == 22, ]$pam_country_accorddate <- "Mozambique (1992-10-04)"
d[d$pam_caseid == 23, ]$pam_country_accorddate <- "South Africa (1993-11-17)"
d[d$pam_caseid == 24, ]$pam_country_accorddate <- "Sudan (2005-01-09)"
d[d$pam_caseid == 25, ]$pam_country_accorddate <- "Lebanon (1989-10-22)"
d[d$pam_caseid == 26, ]$pam_country_accorddate <- "Tajikistan (1997-06-27)"
d[d$pam_caseid == 27, ]$pam_country_accorddate <- "India (1993-02-20)"
d[d$pam_caseid == 28, ]$pam_country_accorddate <- "Bangladesh (1997-12-02)"
d[d$pam_caseid == 29, ]$pam_country_accorddate <- "Nepal (2006-11-21)"
d[d$pam_caseid == 30, ]$pam_country_accorddate <- "Cambodia (1991-10-23)"
d[d$pam_caseid == 31, ]$pam_country_accorddate <- "Philippines (1996-09-02)"
d[d$pam_caseid == 32, ]$pam_country_accorddate <- "Indonesia (2005-08-15)"
d[d$pam_caseid == 33, ]$pam_country_accorddate <- "Timor-Leste (1999-05-05)"
d[d$pam_caseid == 34, ]$pam_country_accorddate <- "Papua New Guinea (2001-08-30)"

####################################
### Describe data
####################################

# Figure 1 (implementation scores over time)

d$pam_country_accorddate2 <- d$pam_country_accorddate

p <- ggplot(d, aes(x = pam_year_count, y = (pam_agg_implem_score + 0.1 * (pam_caseid - 17)), group = pam_country_accorddate)) + 
  geom_line(data = select(d, -pam_country_accorddate), aes(x = pam_year_count, y = (pam_agg_implem_score + 0.1 * (pam_caseid - 17)), group = pam_country_accorddate2), size = 0.25, color = "lightgrey") + 
  geom_line(size = 1) +
  facet_wrap(~ pam_country_accorddate, ncol = 4) + 
  xlab("Post-conflict year") + ylab("Aggregate implementation score") +
  scale_x_continuous(labels = c(1:10), breaks = c(1:10)) +
  theme_jk()

ggsave(p, file = "Output/Figures/implementation_ts_grid.pdf", 
       width = 10, height = 12)

# Figure 2 (histogram of HLIGOs in year of signing)

p <- ggplot(data = filter(d, pam_year_count == 1), aes(x = hligo_lag)) + 
  stat_count() + 
  xlab("Memberships in IGOs with high economic leverage at CPA signing") + ylab("") + 
  scale_x_continuous(breaks = c(0, 2, 4, 6, 8, 10)) + 
  theme_jk()

ggsave(p, file = "Output/Figures/hligo_year0_barplot.pdf", 
       width = 9, height = 5)

# Table A4 (summary statistics)

d_sum <- dplyr::select(d, pam_agg_implem_score,
                       hligo_lag, non_hligo_lag,
                       aidd_comm_last3_lag1,
                       o_totof_last3_lag1,
                       pam_year_count, 
                       pam_powtran_prov, pam_disput_prov, pam_FemSignatories, 
                       pam_warlength_days_log, bdbest_cumsum_log, territorial_conflict, 
                       mediation_cumsum_log,     
                       pam_newSolsSinceCPA, pop_WDI_PW_last3_lag1_log, gdp_WDI_PW_ihst_last3_lag1,
                       growth_WDI_PW_last3_lag1, maxexclpop_EP_last3_lag1, gems_drugs_oil, democracy6_lag, prec_lngdppc_WDI_PW_last3, ucdp_NbCivilWarYes,
                       kappavv_us, UNPKO_Troops_lag_NA, UNPKO_Police_lag_NA,
                       pam_year)

stargazer::stargazer(d_sum, type = "latex", 
                     title = "Summary statistics", digits = 1,
                     median = TRUE, iqr = FALSE, nobs = FALSE, digit.separator = "",
                     out = "Output/Tables/tab_sum.tex",
                     font.size = "scriptsize",
                     label = "tab:sum",
                     covariate.labels = c("Aggregate implementation score",
                                          "IGOs with high economic leverage", "All other IGOs", 
                                          "AidData Foreign aid commitments",
                                          "OECD total official flows ",
                                          "Years after conflict",
                                          "Transitional power sharing government",
                                          "Dispute resolution committee",
                                          "Female signatories",
                                          "Conflict duration in days (logged)",
                                          "Battle deaths during conflict (logged)",
                                          "Territorial conflict",
                                          "Years with mediation  (logged)",
                                          "New leader (with new support coalition) since CPA",
                                          "Population (logged)",
                                          "GDP (IHS transformation, averaged over last 3 years)",
                                          "Economic growth",
                                          "Excluded population",
                                          "Natural resources",
                                          "Democracy (binary)",
                                          "GDP per capita before conflict (logged)",
                                          "Civil war in neighboring country",
                                          "Foreign policy similarity with US",
                                          "UN PKO troops (logged)", "UN PKO police (logged)",
                                          "Year"))

# Figure A1 (compare IC to other countries along all variables)

d$allotherhsigos_lag <- d$hsigo_lag - d$hligo_lag
ic_comp_y1 <- filter(d, pam_year_count == 1, )
ic_comp_y1 <- dplyr::select(ic_comp_y1, 
                  pam_caseid, 
                  pam_agg_implem_score,
                  hligo_lag, non_hligo_lag,
                  aidd_comm_last3_lag1,
                  o_totof_last3_lag1,
                  pam_year_count, 
                  pam_powtran_prov, pam_disput_prov, 
                  pam_warlength_days_log, bdbest_cumsum_log, territorial_conflict, 
                  mediation_cumsum_log,     
                  UNPKO_yes_preCPA, pam_newSolsSinceCPA, pop_WDI_PW_last3_lag1_log, gdp_WDI_PW_ihst_last3_lag1,
                  growth_WDI_PW_last3_lag1, maxexclpop_EP_last3_lag1, gems_drugs_oil, democracy6_lag, prec_lngdppc_WDI_PW_last3, ucdp_NbCivilWarYes,
                  pam_year)

ic_comp_y1$IC <- ifelse(ic_comp_y1$pam_caseid == 11, 1, 0)
ic_comp_y1$IC <- ifelse(is.na(ic_comp_y1$pam_caseid) == TRUE, 0, ic_comp_y1$IC)

ic_comp_y1 <- as.data.frame(ic_comp_y1)
ic_comp_y1$pam_caseid <- NULL
ic_comp_y1$pam_year <- NULL

ic_comp_y1$pam_agg_implem_score <- meanstd(ic_comp_y1$pam_agg_implem_score)
ic_comp_y1$hligo_lag <- meanstd(ic_comp_y1$hligo_lag)
ic_comp_y1$non_hligo_lag <- meanstd(ic_comp_y1$non_hligo_lag)
ic_comp_y1$aidd_comm_last3_lag1 <- meanstd(log(ic_comp_y1$aidd_comm_last3_lag1 + 1))
ic_comp_y1$o_totof_last3_lag1 <- meanstd(log(ic_comp_y1$o_totof_last3_lag1 + 1))
ic_comp_y1$pam_warlength_days_log <- meanstd(ic_comp_y1$pam_warlength_days_log)
ic_comp_y1$bdbest_cumsum_log <- meanstd(ic_comp_y1$bdbest_cumsum_log)
ic_comp_y1$mediation_cumsum_log <- meanstd(ic_comp_y1$mediation_cumsum_log)
ic_comp_y1$pop_WDI_PW_last3_lag1_log  <- meanstd(ic_comp_y1$pop_WDI_PW_last3_lag1_log)
ic_comp_y1$gdp_WDI_PW_ihst_last3_lag1 <- meanstd(ic_comp_y1$gdp_WDI_PW_ihst_last3_lag1)
ic_comp_y1$growth_WDI_PW_last3_lag1 <- meanstd(ic_comp_y1$growth_WDI_PW_last3_lag1)
ic_comp_y1$maxexclpop_EP_last3_lag1 <- meanstd(ic_comp_y1$maxexclpop_EP_last3_lag1)
ic_comp_y1$prec_lngdppc_WDI_PW_last3 <- meanstd(ic_comp_y1$prec_lngdppc_WDI_PW_last3)
ic_comp_y1$pam_powtran_prov <- ifelse(ic_comp_y1$pam_powtran_prov == 0, -0.5, 0.5)
ic_comp_y1$pam_disput_prov <- ifelse(ic_comp_y1$pam_disput_prov == 0, -0.5, 0.5)
ic_comp_y1$territorial_conflict <- ifelse(ic_comp_y1$territorial_conflict == 0, -0.5, 0.5)
ic_comp_y1$UNPKO_yes_preCPA <- ifelse(ic_comp_y1$UNPKO_yes_preCPA == 0, -0.5, 0.5)
ic_comp_y1$pam_newSolsSinceCPA <- ifelse(ic_comp_y1$pam_newSolsSinceCPA == 0, -0.5, 0.5)
ic_comp_y1$gems_drugs_oil <- ifelse(ic_comp_y1$gems_drugs_oil == 0, -0.5, 0.5)
ic_comp_y1$democracy6_lag <- ifelse(ic_comp_y1$democracy6_lag == 0, -0.5, 0.5)
ic_comp_y1$ucdp_NbCivilWarYes <- ifelse(ic_comp_y1$ucdp_NbCivilWarYes == 0, -0.5, 0.5)
 
names(ic_comp_y1) <- c("Aggregate implementation score",
                       "IGOs with high economic leverage", "All other IGOs", 
                       "AidData Foreign aid commitments",
                       "OECD total official flows ",
                       "Years after conflict",
                       "Transitional power sharing government",
                       "Dispute resolution committee",
                       "Conflict duration in days (logged)",
                       "Battle deaths during conflict (logged)",
                       "Territorial conflict",
                       "Years with mediation  (logged)",
                       "UN PKO before CPA",
                       "New leader (with new support coalition) since CPA",
                       "Population (logged)",
                       "GDP (IHS transformation, averaged over last 3 years)",
                       "Economic growth",
                       "Excluded population",
                       "Natural resources",
                       "Democracy (binary)",
                       "GDP per capita before conflict (logged)",
                       "Civil war in neighboring country", 
                       "IC")

ic_comp_long <- pivot_longer(data = ic_comp_y1, cols = !IC, names_to = "variable", values_to = "value")
ic_comp_long$variable_pos <- as.numeric(as.factor(ic_comp_long$variable))
ic_comp_long$country <- ifelse(ic_comp_long$IC == 1, "ci", NA)

p <- ggplot(data = filter(ic_comp_long, IC == 0), aes(x = value, y = variable)) +
  geom_density_ridges(color = NA, fill = "gray", stat = "binline", bins = 20, scale = 1.1) +
  xlim(-1.5, 1.5) +
  geom_point(data = filter(ic_comp_long, IC == 1), aes(x = value, y = variable_pos + 0.25), color = "black", size = 3) +
  theme_minimal() + 
  theme(text = element_text(color = "black"), axis.text.y = element_text(color = "black"), axis.text.x = element_text(color = "black")) + 
  ylab("") + xlab("") + 
  scale_x_continuous(breaks = c(-1, 0, 1), labels = c("2 SD below mean", "Mean", "2 SD above mean"))
 
ggsave(p, file = "Output/Figures/ic_comparison_allvars.pdf", width = 7, height = 7)

# Figure A2 (aid to Ivory Coast)

p <- ggplot(data = filter(d, pam_caseid == 11), aes(x = pam_year, y = aidd_comm_last3_lag1_sp)) + 
  geom_line() + 
  theme_jk() + 
  xlab("") + ylab("Standardized values") + 
  labs(title = "AidData foreign aid commitments to Ivory Coast, 2007-2015",
       subtitle = "Values are averaged over 3 prior years")

ggsave(p, file = "Output/Figures/ic_aid.pdf", width = 7, height = 5)

####################################
### Regressions
####################################

# General approach

# 4 main models per explanatory variable variable
# m_hligo_rewb, m_hligo_ar, m_hligo_cfer, m_hligo_ivcyfe
# m_aidd_comm_rewb, m_aidd_comm_ar, m_aidd_comm_cfer, m_aidd_comm_ivcyfe

# then additional sets of robustness checks:

# (1) one with UNGA, plus all UNPKO personnel in one variable
# m_hligo_rewb_r1, m_hligo_ar_r1, m_hligo_cfer_r1
# m_aidd_comm_rewb_r1, m_aidd_comm_ar_r1, m_aidd_comm_cfer_r1

# (2) one with UNGA and separate UNPKO personnel
# m_hligo_rewb_r2, m_hligo_ar_r2, m_hligo_cfer_r2
# m_aidd_comm_rewb_r2, m_aidd_comm_ar_r2, m_aidd_comm_cfer_r2

# (3) OECD instead of AidData aid
# m_aidd_comm_rewb_r3, m_aidd_comm_rewb_r3

# (4) separate indicators for country-specific aid
# m_aidd_comm_cfer_r4, m_aidd_comm_cfer_r5

# Main models: HLIGOs -----------------------------------------------------

# T1M1 REWB

# Skeleton for the REWB model: 
# y_{ij} = \beta_0 + \beta_1(x_{ij}-\bar{x}_j) + \beta_4 \bar{x}_j + \beta_2 z_j + (u_j + \epsilon_{ij})

summary(m_hligo_rewb <- lmer(pam_agg_implem_score ~ 
                               hligo_lag_sp_w + hligo_lag_sp_b +
                               non_hligo_lag_sp_w + non_hligo_lag_sp_b +
                               log(pam_year_count) +
                               pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                               pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                               mediation_cumsum_log +    
                               pam_newSolsSinceCPA +
                               pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                               prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                               kappavv_us + 
                               log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                               factor(region) +
                               (1 | pam_caseid) + (1 | pam_year_count),
                             data = d))

# T1M2 PCSEs, AR1

summary(m_hligo_ar <- panelAR(pam_agg_implem_score ~ 
                                hligo_lag_sp + non_hligo_lag_sp + 
                                log(pam_year_count) +
                                pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                mediation_cumsum_log +
                                pam_newSolsSinceCPA +
                                pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                kappavv_us + 
                                log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                factor(region),
                              data = d,
                              panelVar = "pam_caseid",
                              timeVar = "pam_year_count",
                              autoCorr = "psar1",
                              rhotype = "dw",
                              dof.correction = FALSE,
                              panelCorrMethod = "pcse"))

# T1M3 FEs for countries, robust SEs

summary(m_hligo_cfer <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                              hligo_lag_sp + non_hligo_lag_sp + 
                                              log(pam_year_count) + 
                                              pam_powtran_prov +
                                              pam_warlength_days_log + 
                                              mediation_cumsum_log +
                                              pam_newSolsSinceCPA +
                                              pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + 
                                              democracy6_lag +
                                              ucdp_NbCivilWarYes +
                                              kappavv_us + 
                                              log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                            fixed_effects = ~ pam_country,
                                            se_type = "HC2",
                                            data = d))


# T1M4 IV (with two-way FEs)

summary(m_hligo_ivcyfe <- estimatr::iv_robust(pam_agg_implem_score ~ 
                                                hligo_lag + non_hligo_lag | 
                                                hligo_region_lag + I(alligo_region_lag - hligo_region_lag),
                                              fixed_effects = ~ pam_year_count + pam_caseid,
                                              se_type = "HC1",
                                              diagnostics = TRUE,
                                              data = d))

# Main models: Aid -----------------------------------------------------

# T2M1 REWB

summary(m_aidd_comm_rewb <- lmer(pam_agg_implem_score ~ 
                                   aidd_comm_last3_lag1_sp_w + aidd_comm_last3_lag1_sp_b +
                                   log(pam_year_count) +
                                   pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                   pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                                   mediation_cumsum_log +    
                                   pam_newSolsSinceCPA +
                                   pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                   maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                                   prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                                   kappavv_us + 
                                   log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                   factor(region) +
                                   (1 | pam_caseid) + (1 | pam_year_count),
                                 data = d))

# T2M2 PCSEs, AR1

summary(m_aidd_comm_ar <- panelAR(pam_agg_implem_score ~ 
                                    aidd_comm_last3_lag1_sp + 
                                    log(pam_year_count) + 
                                    pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                    pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                    mediation_cumsum_log +
                                    pam_newSolsSinceCPA +
                                    pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                    maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                    prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                    kappavv_us + 
                                    log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                    factor(region),
                                  data = d,
                                  panelVar = "pam_caseid",
                                  timeVar = "pam_year_count",
                                  autoCorr = "psar1",
                                  rhotype = "dw",
                                  dof.correction = FALSE,
                                  panelCorrMethod = "pcse"))

# T2M3 FEs for countries, robust SEs

summary(m_aidd_comm_cfer <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                  aidd_comm_last3_lag1_sp + 
                                                  log(pam_year_count) + 
                                                  pam_powtran_prov + pam_disput_prov + 
                                                  pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                                  mediation_cumsum_log +
                                                  pam_newSolsSinceCPA +
                                                  pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                                  maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                                  prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                                  kappavv_us + 
                                                  log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                                fixed_effects = ~ pam_country,
                                                se_type = "HC2",
                                                data = d))

# T2M4 IV (with two-way FEs)

summary(m_aidd_comm_ivcyfe <- estimatr::iv_robust(pam_agg_implem_score ~ 
                                                    aidd_comm_GDP_ln_last3_lag1 | 
                                                    aidIV_frac1_last3_lag1, 
                                                  fixed_effects = ~ pam_year_count + pam_caseid,
                                                  se_type = "classical",
                                                  diagnostics = TRUE,
                                                  data = d))


# Robustness: HLIGOs ------------------------------------------------------

# Main models without control variables ------------------------------------------------------

# TA6M1 REWB

summary(m_hligo_rewb_nocontrols <- lmer(pam_agg_implem_score ~ 
                                          hligo_lag_sp_w + hligo_lag_sp_b +
                                          log(pam_year_count) +
                                          factor(region) +
                                          (1 | pam_caseid) + (1 | pam_year_count),
                                        data = d))

# TA6M2 PCSEs, AR1

summary(m_hligo_ar_nocontrols <- panelAR(pam_agg_implem_score ~ 
                                           hligo_lag_sp + 
                                           log(pam_year_count) +
                                           factor(region),
                                         data = d,
                                         panelVar = "pam_caseid",
                                         timeVar = "pam_year_count",
                                         autoCorr = "psar1",
                                         rhotype = "dw",
                                         dof.correction = FALSE,
                                         panelCorrMethod = "pcse"))

# TA6M3 FEs for countries, robust SEs

summary(m_hligo_cfer_nocontrols <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                         hligo_lag_sp + 
                                                         log(pam_year_count), 
                                                       fixed_effects = ~ pam_country,
                                                       se_type = "HC2",
                                                       data = d))

# Other robustness tests ------------------------------------------------------

# TA9M1 REWB

summary(m_hligo_rewb_r1 <- lmer(pam_agg_implem_score ~ 
                                  hligo_lag_sp_w + hligo_lag_sp_b +
                                  non_hligo_lag_sp_w + non_hligo_lag_sp_b +
                                  log(pam_year_count) +
                                  pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                  pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                                  mediation_cumsum_log +    
                                  pam_newSolsSinceCPA +
                                  pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                                  prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                                  unga_diff_avgo_lag_std + unga_diff_china_lag_std +
                                  log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                  factor(region) +
                                  (1 | pam_caseid) + (1 | pam_year_count),
                                data = d))

# TA10M1 REWB

summary(m_hligo_rewb_r2 <- lmer(pam_agg_implem_score ~ 
                                  hligo_lag_sp_w + hligo_lag_sp_b +
                                  non_hligo_lag_sp_w + non_hligo_lag_sp_b +
                                  log(pam_year_count) +
                                  pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                  pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                                  mediation_cumsum_log +    
                                  pam_newSolsSinceCPA +
                                  pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                                  prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                                  kappavv_us +
                                  log(UNPKO_Troops_lag_NA + 1) + log(UNPKO_Police_lag_NA + 1) + 
                                  factor(region) +
                                  (1 | pam_caseid) + (1 | pam_year_count),
                                data = d))

# TA9M2 PCSEs, AR1

summary(m_hligo_ar_r1 <- panelAR(pam_agg_implem_score ~ 
                                   hligo_lag_sp + non_hligo_lag_sp + 
                                   log(pam_year_count) +
                                   pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                   pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                   mediation_cumsum_log +
                                   pam_newSolsSinceCPA +
                                   pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                   prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                   unga_diff_avgo_lag_std + unga_diff_china_lag_std +
                                   log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                   factor(region),
                                 data = d,
                                 panelVar = "pam_caseid",
                                 timeVar = "pam_year_count",
                                 autoCorr = "psar1",
                                 rhotype = "dw",
                                 dof.correction = FALSE,
                                 panelCorrMethod = "pcse"))

# TA10M2 PCSEs, AR1

summary(m_hligo_ar_r2 <- panelAR(pam_agg_implem_score ~ 
                                   hligo_lag_sp + non_hligo_lag_sp + 
                                   log(pam_year_count) +
                                   pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                   pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                   mediation_cumsum_log +
                                   pam_newSolsSinceCPA +
                                   pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                   prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                   kappavv_us + 
                                   log(UNPKO_Troops_lag_NA + 1) + log(UNPKO_Police_lag_NA + 1) + 
                                   factor(region),
                                 data = d,
                                 panelVar = "pam_caseid",
                                 timeVar = "pam_year_count",
                                 autoCorr = "psar1",
                                 rhotype = "dw",
                                 dof.correction = FALSE,
                                 panelCorrMethod = "pcse"))

# TA9M3 FEs for countries, robust SEs

summary(m_hligo_cfer_r1 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                 hligo_lag_sp + non_hligo_lag_sp + 
                                                 log(pam_year_count) + 
                                                 pam_powtran_prov + 
                                                 pam_warlength_days_log + 
                                                 mediation_cumsum_log +
                                                 pam_newSolsSinceCPA +
                                                 pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + 
                                                 democracy6_lag +
                                                 ucdp_NbCivilWarYes +
                                                 unga_diff_avgo_lag_std + unga_diff_china_lag_std +  
                                                 log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                               fixed_effects = ~ pam_country,
                                               se_type = "HC2",
                                               data = d))

# TA10M3 FEs for countries, robust SEs

summary(m_hligo_cfer_r2 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                 hligo_lag_sp + non_hligo_lag_sp + 
                                                 log(pam_year_count) + 
                                                 pam_powtran_prov + 
                                                 pam_warlength_days_log + 
                                                 mediation_cumsum_log +
                                                 pam_newSolsSinceCPA +
                                                 pop_WDI_PW_last3_lag1_log + growth_WDI_PW_last3_lag1 + maxexclpop_EP_last3_lag1 + 
                                                 democracy6_lag +
                                                 ucdp_NbCivilWarYes +
                                                 kappavv_us + 
                                                 log(UNPKO_Troops_lag_NA + 1) + log(UNPKO_Police_lag_NA + 1), 
                                               fixed_effects = ~ pam_country,
                                               se_type = "HC2",
                                               data = d))

# Robustness: Aid ------------------------------------------------------

# Main models wihtout control variables -----------------------------------------------------

# TA8M1 REWB

summary(m_aidd_comm_rewb_nocontrols <- lmer(pam_agg_implem_score ~ 
                                              aidd_comm_last3_lag1_sp_w + aidd_comm_last3_lag1_sp_b +
                                              log(pam_year_count) +
                                              factor(region) +
                                              (1 | pam_caseid) + (1 | pam_year_count),
                                            data = d))

# TA8M2 PCSEs, AR1

summary(m_aidd_comm_ar_nocontrols <- panelAR(pam_agg_implem_score ~ 
                                               aidd_comm_last3_lag1_sp + 
                                               log(pam_year_count) + 
                                               factor(region),
                                             data = d,
                                             panelVar = "pam_caseid",
                                             timeVar = "pam_year_count",
                                             autoCorr = "psar1",
                                             rhotype = "dw",
                                             dof.correction = FALSE,
                                             panelCorrMethod = "pcse"))

# TA8M3 FEs for countries, robust SEs

summary(m_aidd_comm_cfer_nocontrols <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                             aidd_comm_last3_lag1_sp + 
                                                             log(pam_year_count), 
                                                           fixed_effects = ~ pam_country,
                                                           se_type = "HC2",
                                                           data = d))

# Other robustness tests -----------------------------------------------------

# TA11M1 REWB

summary(m_aidd_comm_rewb_r1 <- lmer(pam_agg_implem_score ~ 
                                      aidd_comm_last3_lag1_sp_w + aidd_comm_last3_lag1_sp_b +
                                      log(pam_year_count) +
                                      pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                      pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                                      mediation_cumsum_log +    
                                      pam_newSolsSinceCPA +
                                      pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                      maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                                      prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                                      unga_diff_avgo_lag_std + unga_diff_china_lag_std +
                                      log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                      factor(region) +
                                      (1 | pam_caseid) + (1 | pam_year_count),
                                    data = d))

# TA12M1 REWB

summary(m_aidd_comm_rewb_r2 <- lmer(pam_agg_implem_score ~ 
                                      aidd_comm_last3_lag1_sp_w + aidd_comm_last3_lag1_sp_b +
                                      log(pam_year_count) +
                                      pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                      pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                                      mediation_cumsum_log +    
                                      pam_newSolsSinceCPA +
                                      pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                      maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                                      prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                                      kappavv_us + 
                                      log(UNPKO_Troops_lag_NA + 1) + log(UNPKO_Police_lag_NA + 1) + 
                                      factor(region) +
                                      (1 | pam_caseid) + (1 | pam_year_count),
                                    data = d))

# TA13M1 REWB

summary(m_aidd_comm_rewb_r3 <- lmer(pam_agg_implem_score ~ 
                                      o_totof_last3_lag1_sp_w + o_totof_last3_lag1_sp_b +
                                      log(pam_year_count) +
                                      pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                      pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +  
                                      mediation_cumsum_log +    
                                      pam_newSolsSinceCPA +
                                      pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                      maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag + 
                                      prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes +
                                      kappavv_us + 
                                      log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                      factor(region) +
                                      (1 | pam_caseid) + (1 | pam_year_count),
                                    data = d))

# TA11M2 PCSEs, AR1

summary(m_aidd_comm_ar_r1 <- panelAR(pam_agg_implem_score ~ 
                                       aidd_comm_last3_lag1_sp + 
                                       log(pam_year_count) + 
                                       pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                       pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                       mediation_cumsum_log +
                                       pam_newSolsSinceCPA +
                                       pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                       maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                       prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                       unga_diff_avgo_lag_std + unga_diff_china_lag_std +
                                       log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                       factor(region),
                                     data = d,
                                     panelVar = "pam_caseid",
                                     timeVar = "pam_year_count",
                                     autoCorr = "psar1",
                                     rhotype = "dw",
                                     dof.correction = FALSE,
                                     panelCorrMethod = "pcse"))

# TA12M2 PCSEs, AR1

summary(m_aidd_comm_ar_r2 <- panelAR(pam_agg_implem_score ~ 
                                       aidd_comm_last3_lag1_sp + 
                                       log(pam_year_count) + 
                                       pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                       pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                       mediation_cumsum_log +
                                       pam_newSolsSinceCPA +
                                       pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                       maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                       prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                       kappavv_us + 
                                       log(UNPKO_Troops_lag_NA + 1) + log(UNPKO_Police_lag_NA + 1) + 
                                       factor(region),
                                     data = d,
                                     panelVar = "pam_caseid",
                                     timeVar = "pam_year_count",
                                     autoCorr = "psar1",
                                     rhotype = "dw",
                                     dof.correction = FALSE,
                                     panelCorrMethod = "pcse"))

# TA13M2 PCSEs, AR1

summary(m_aidd_comm_ar_r3 <- panelAR(pam_agg_implem_score ~ 
                                       o_totof_last3_lag1_sp + 
                                       log(pam_year_count) + 
                                       pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                       pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                       mediation_cumsum_log +
                                       pam_newSolsSinceCPA +
                                       pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                       maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                       prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                       kappavv_us + 
                                       log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1) + 
                                       factor(region),
                                     data = d,
                                     panelVar = "pam_caseid",
                                     timeVar = "pam_year_count",
                                     autoCorr = "psar1",
                                     rhotype = "dw",
                                     dof.correction = FALSE,
                                     panelCorrMethod = "pcse"))

# TA11M3 FEs for countries, robust SEs

summary(m_aidd_comm_cfer_r1 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                     aidd_comm_last3_lag1_sp + 
                                                     log(pam_year_count) + 
                                                     pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                                     pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                                     mediation_cumsum_log +
                                                     pam_newSolsSinceCPA +
                                                     pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                                     maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                                     prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                                     unga_diff_avgo_lag_std + unga_diff_china_lag_std +
                                                     log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                                   fixed_effects = ~ pam_country,
                                                   se_type = "HC2",
                                                   data = d))

# TA12M3 FEs for countries, robust SEs

summary(m_aidd_comm_cfer_r2 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                     aidd_comm_last3_lag1_sp + 
                                                     log(pam_year_count) + 
                                                     pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                                     pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                                     mediation_cumsum_log +
                                                     pam_newSolsSinceCPA +
                                                     pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                                     maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                                     prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                                     kappavv_us + 
                                                     log(UNPKO_Troops_lag_NA + 1) + log(UNPKO_Police_lag_NA + 1), 
                                                   fixed_effects = ~ pam_country,
                                                   se_type = "HC2",
                                                   data = d))

# TA13M3 FEs for countries, robust SEs

summary(m_aidd_comm_cfer_r3 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                     o_totof_last3_lag1_sp + 
                                                     log(pam_year_count) + 
                                                     pam_powtran_prov + pam_disput_prov + pam_FemSignatories + 
                                                     pam_warlength_days_log + bdbest_cumsum_log + territorial_conflict +
                                                     mediation_cumsum_log +
                                                     pam_newSolsSinceCPA +
                                                     pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                                     maxexclpop_EP_last3_lag1 + gems_drugs_oil + democracy6_lag +
                                                     prec_lngdppc_WDI_PW_last3 + ucdp_NbCivilWarYes + 
                                                     kappavv_us + 
                                                     log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                                   fixed_effects = ~ pam_country,
                                                   se_type = "HC2",
                                                   data = d))

# TA14M1 FEs for countries, robust SEs

summary(m_aidd_comm_cfer_r4 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                     aidd_comm_last3_lag1_sp + 
                                                     log(aidd_comm_us/1000000 + 1) + log(aidd_comm_france/1000000 + 1) + log(aidd_comm_uk/1000000 + 1) +
                                                     log(pam_year_count) + 
                                                     pam_powtran_prov + 
                                                     pam_warlength_days_log + 
                                                     mediation_cumsum_log +
                                                     pam_newSolsSinceCPA +
                                                     pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                                     maxexclpop_EP_last3_lag1 + 
                                                     democracy6_lag +
                                                     ucdp_NbCivilWarYes + 
                                                     kappavv_us + 
                                                     log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                                   fixed_effects = ~ pam_country,
                                                   se_type = "HC2",
                                                   data = d))

# TA14M2 FEs for countries, robust SEs

summary(m_aidd_comm_cfer_r5 <- estimatr::lm_robust(pam_agg_implem_score ~ 
                                                     log(aidd_comm_us/1000000 + 1) + log(aidd_comm_france/1000000 + 1) + log(aidd_comm_uk/1000000 + 1) +
                                                     log(pam_year_count) + 
                                                     pam_powtran_prov + 
                                                     pam_warlength_days_log + 
                                                     mediation_cumsum_log +
                                                     pam_newSolsSinceCPA +
                                                     pop_WDI_PW_last3_lag1_log + gdp_WDI_PW_ihst_last3_lag1 + growth_WDI_PW_last3_lag1 + 
                                                     maxexclpop_EP_last3_lag1 + 
                                                     democracy6_lag +
                                                     ucdp_NbCivilWarYes + 
                                                     kappavv_us + 
                                                     log(I(UNPKO_Troops_lag_NA + UNPKO_Police_lag_NA ) + 1), 
                                                   fixed_effects = ~ pam_country,
                                                   se_type = "HC2",
                                                   data = d))


# Tables ------------------------------------------------------------------

# Note: I rearranged some rows in the .tex files by hand for readability, but the 
# results are created with the code below.

# Table 1 (full version: A5)

texreg(list(m_hligo_rewb, extract.panelAR(m_hligo_ar), extract(m_hligo_cfer, include.ci = FALSE), extract(m_hligo_ivcyfe, include.ci = FALSE)),
       file = "Output/Tables/tab_m_hligo.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "IGOs with high economic leverage (within effect)",
                             "IGOs with high economic leverage (between effect)",
                             "All other IGOs (within effect)",
                             "All other IGOs (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US",
                             "UN PKO personnel (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "IGOs with high economic leverage (composite effect)",
                             "All other IGOs (composite effect)",
                             "IGOs with high economic leverage (IV, composite effect)",
                             "All other IGOs (IV, composite effect)"),
       reorder.coef = c(2, 3, 29, 31, 4, 5, 30, 32, 6:28, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 27:31),
       center = TRUE,
       caption = "Regression estimates of the association between IGO memberships and CPA implementation at the post-CPA year level. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country FE and robust SEs",
                              "HLIGOs instrumented plus country and year FEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_hligo",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# Table 2 (full version: A7)

texreg(list(m_aidd_comm_rewb, extract.panelAR(m_aidd_comm_ar), extract(m_aidd_comm_cfer, include.ci = FALSE), extract(m_aidd_comm_ivcyfe, include.ci = FALSE)),
       file = "Output/Tables/tab_m_aidd_comm.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "AidData Foreign aid commitments (within effect)",
                             "AidData Foreign aid commitments (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "GDP (IHS transformation, lagged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US",
                             "UN PKO personnel (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "AidData Foreign aid commitments (composite effect)",
                             "AidData Foreign aid commitments (IV, within effect)"),
       reorder.coef = c(2, 3, 28, 29, 4:27, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 24:28),
       center = TRUE,
       caption = "Regression estimates of the association between prior foreign aid (using aid commitment volumes from AidData) and CPA implementation at the post-CPA year level. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country fixed effects and robust SEs",
                              "Aid instrumented plus country and year FEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_aidd_comm",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# Tables for robustness tests ------------------------------------------------------

# No controls

# Table A6

texreg(list(m_hligo_rewb_nocontrols, extract.panelAR(m_hligo_ar_nocontrols), extract(m_hligo_cfer_nocontrols, include.ci = FALSE)),
       file = "Output/Tables/tab_m_hligo_nocontrols.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "IGOs with high economic leverage (within effect)",
                             "IGOs with high economic leverage (between effect)",
                             "Years since conflict (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "IGOs with high economic leverage (composite effect)"),
       reorder.coef = c(10, 2, 3, 4, 5:9, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 5:9),
       center = TRUE,
       caption = "Regression estimates of the association between IGO memberships and CPA implementation at the post-CPA year level, without control variables. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country fixed effects and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_hligo_nocontrols",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# Table A8

texreg(list(m_aidd_comm_rewb_nocontrols, extract.panelAR(m_aidd_comm_ar_nocontrols), extract(m_aidd_comm_cfer_nocontrols, include.ci = FALSE)),
       file = "Output/Tables/tab_m_aidd_comm_nocontrols.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "AidData Foreign aid commitments (within effect)",
                             "AidData Foreign aid commitments (between effect)",
                             "Years since conflict (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "AidData Foreign aid commitments (composite effect)"),
       reorder.coef = c(2, 3, 10, 4, 5:9, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 5:9),
       center = TRUE,
       caption = "Regression estimates of the association between prior foreign aid (using aid commitment volumes from AidData) and CPA implementation at the post-CPA year level, without control variables. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country fixed effects and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_aidd_comm_nocontrols",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# (1) one with UNGA, plus all UNPKO personnel in one variable

# Table A9 (m_hligo_rewb_r1, m_hligo_ar_r1, m_hligo_cfer_r1)

texreg(list(m_hligo_rewb_r1, extract.panelAR(m_hligo_ar_r1), extract(m_hligo_cfer_r1, include.ci = FALSE)),
       file = "Output/Tables/tab_m_hligo_r1.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "IGOs with high economic leverage (within effect)",
                             "IGOs with high economic leverage (between effect)",
                             "All other IGOs (within effect)",
                             "All other IGOs (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US/UK/France",
                             "Foreign policy similarity with China",
                             "UN PKO personnel (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "IGOs with high economic leverage (composite effect)",
                             "All other IGOs (composite effect)"),
       reorder.coef = c(2, 3, 30, 4, 5, 31, 6:29, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 26:30),
       center = TRUE,
       caption = "Regression estimates of the association between IGO memberships and CPA implementation at the post-CPA year level. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country FE and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_hligo_r1",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# Table A11 (m_aidd_comm_rewb_r1, m_aidd_comm_ar_r1, m_aidd_comm_cfer_r1)

texreg(list(m_aidd_comm_rewb_r1, extract.panelAR(m_aidd_comm_ar_r1), extract(m_aidd_comm_cfer_r1, include.ci = FALSE)),
       file = "Output/Tables/tab_m_aidd_comm_r1.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "AidData Foreign aid commitments (within effect)",
                             "AidData Foreign aid commitments (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "GDP (IHS transformation, lagged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US/UK/France",
                             "Foreign policy similarity with China",
                             "UN PKO personnel (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "AidData Foreign aid commitments (composite effect)"),
       reorder.coef = c(2, 3, 29, 4:28, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 24:28),
       center = TRUE,
       caption = "Regression estimates of the association between prior foreign aid (using aid commitment volumes from AidData) and CPA implementation at the post-CPA year level, without control variables. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country FE and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_aidd_comm_r1",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# (2) one with UNGA and separate UNPKO personnel

# Table A10 (m_hligo_rewb_r2, m_hligo_ar_r2, m_hligo_cfer_r2)

texreg(list(m_hligo_rewb_r2, extract.panelAR(m_hligo_ar_r2), extract(m_hligo_cfer_r2, include.ci = FALSE)),
       file = "Output/Tables/tab_m_hligo_r2.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "IGOs with high economic leverage (within effect)",
                             "IGOs with high economic leverage (between effect)",
                             "All other IGOs (within effect)",
                             "All other IGOs (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US",
                             "UN PKO troops (logged)",
                             "UN PKO police (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "IGOs with high economic leverage (composite effect)",
                             "All other IGOs (composite effect)"),
       reorder.coef = c(2, 3, 30, 4, 5, 31, 6:29, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 26:30),
       center = TRUE,
       caption = "Regression estimates of the association between IGO memberships and CPA implementation at the post-CPA year level. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country FE and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_hligo_r2",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# Table A12 (m_aidd_comm_rewb_r2, m_aidd_comm_ar_r2, m_aidd_comm_cfer_r2)

texreg(list(m_aidd_comm_rewb_r2, extract.panelAR(m_aidd_comm_ar_r2), extract(m_aidd_comm_cfer_r2, include.ci = FALSE)),
       file = "Output/Tables/tab_m_aidd_comm_r2.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "AidData Foreign aid commitments (within effect)",
                             "AidData Foreign aid commitments (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "GDP (IHS transformation, lagged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US",
                             "UN PKO troops (logged)",
                             "UN PKO police (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "AidData Foreign aid commitments (composite effect)"),
       reorder.coef = c(2, 3, 29, 4:28, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 24:28),
       center = TRUE,
       caption = "Regression estimates of the association between prior foreign aid (using aid commitment volumes from AidData) and CPA implementation at the post-CPA year level, without control variables. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country FE and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_aidd_comm_r2",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# (3) OECD instead of AidData aid

# Table A13 (m_aidd_comm_rewb_r3, m_aidd_comm_ar_r3, m_aidd_comm_cfer_r3)

texreg(list(m_aidd_comm_rewb_r3, extract.panelAR(m_aidd_comm_ar_r3), extract(m_aidd_comm_cfer_r3, include.ci = FALSE)),
       file = "Output/Tables/tab_m_aidd_comm_r3.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("Intercept",
                             "OECD Foreign aid commitments (within effect)",
                             "OECD Foreign aid commitments (between effect)",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Dispute resolution committee",
                             "Female signatories",
                             "Conflict duration (logged)",
                             "Battle deaths during conflict (logged)",
                             "Territorial conflict",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "GDP (IHS transformation, lagged)",
                             "Economic growth",
                             "Excluded population",
                             "Natural resources",
                             "Democracy",
                             "GDP per capita before conflict (logged)",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US",
                             "UN PKO personnel (logged)",
                             "Europe & Central Asia",
                             "Latin America & Caribbean",
                             "Middle East & North Africa",
                             "South Asia",
                             "Sub-Saharan Africa",
                             "OECD Foreign aid commitments (composite effect)"),
       reorder.coef = c(2, 3, 28, 4:27, 1),
       groups = list("Region fixed effects (baseline: East Asia)" = 23:27),
       center = TRUE,
       caption = "Regression estimates of the association between prior foreign aid (using aid commitment volumes from AidData) and CPA implementation at the post-CPA year level. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Within-Between random effects for post-conflict periods",
                              "Prais-Winsten FGLS with conflict-specific AR(1) autocorrelation and PCSEs", 
                              "Country fixed effects and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test).",
       label = "tab:m_aidd_comm_r3",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")

# (4) separate indicators for country-specific aid
# Table A14 (m_aidd_comm_cfer_r4, m_aidd_comm_cfer_r4)

texreg(list(extract(m_aidd_comm_cfer_r4, include.ci = FALSE), extract(m_aidd_comm_cfer_r5, include.ci = FALSE)),
       file = "Output/Tables/tab_m_aidd_comm_r45.tex",
       single.row = FALSE,
       stars = c(0.05),
       custom.coef.names = c("AidData Foreign aid commitments (all)",
                             "AidData Foreign aid commitments from United States",
                             "AidData Foreign aid commitments from France",
                             "AidData Foreign aid commitments from United Kingdom",
                             "Years since conflict (logged)",
                             "Transitional power sharing government",
                             "Conflict duration (logged)",
                             "Years with mediation  (logged)",
                             "New leader since CPA",
                             "Population (logged)",
                             "GDP (IHS transformation, lagged)",
                             "Economic growth",
                             "Excluded population",
                             "Democracy",
                             "Civil war in neighboring country",
                             "Foreign policy similarity with US",
                             "UN PKO personnel (logged)"),
       center = TRUE,
       caption = "Regression estimates of the association between prior foreign aid (using aid commitment volumes from AidData) and CPA implementation at the post-CPA year level. Implementation scores range from 2 to 95. See text for detailed description of estimators.",
       caption.above = TRUE,
       custom.model.names = c("Country fixed effects and robust SEs", 
                              "Country fixed effects and robust SEs"),
       custom.note = "Standard errors in parentheses. $^* p < 0.05$ (two-tailed test). Only predictors that vary by country are included.",
       label = "tab:m_aidd_comm_r45",
       booktabs = TRUE,
       dcolumn = TRUE,
       use.packages = FALSE,
       fontsize = "scriptsize")